<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购买商品</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="css/buygoods.css"/>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/jquery.cookie.js"></script>
</head>
<body>

    <div class="container" id="returnindex">
        <div class="row">
            <div class="col-xs-9 col-sm-9 col-md-9 col-lg-9"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="goodsmanage"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="ordermanage"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"><a href="index.html">返回首页</a></div>
        </div>
    </div>
    <div class="container" id="pagetitle">
        <div class="row">
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
            <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3"><p id="titlep"><span>乐享生活</span>&nbsp;购买商品</p></div>
        </div>
    </div>

    <div class="container" id="title">
        <div class="row">
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
            <div class="col-xs-10 col-sm-10 col-md-10 col-lg-10" id="titleten">
                <div id="bordertop">确认订单信息</div>
            </div>
        </div>
    </div>

    <div class="container" id="order">
        <div class="row">
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
            <div class="col-xs-10 col-sm-10 col-md-10 col-lg-10" id="orderten"></div>
        </div>
    </div>

    <script type="text/javascript">

        var goodsid = location.search.split("=")[1].split("&")[0];
        var buynum = location.search.split("=")[2];
        var userId = $.cookie("userId");

        $.post("goods/getById",{goodsid:goodsid},function (data1) {
            data1 = JSON.parse(data1);
            $.get("address/findMyAddress",{userId:userId},function (data2) {

                data2 = JSON.parse(data2);

                var address = '';

                for (var i = 0; i < data2.length; i++) {
                    address += '<input class="check" value="'+ data2[i].id +'" type="radio" name="address">&nbsp;&nbsp;'+ data2[i].receiveName +'&nbsp;&nbsp;&nbsp;'+ data2[i].telephone +'&nbsp;&nbsp;&nbsp;'+ data2[i].province +'&nbsp;&nbsp;&nbsp;'+ data2[i].city +'&nbsp;&nbsp;&nbsp;'+ data2[i].details +'<br>\n';
                }

                var ht = '<div class="row titlerow">\n' +
                    '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 titlediv">商品名称</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 titlediv">商品图片</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 titlediv">单价</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 titlediv">数量</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 titlediv">小计</div>\n' +
                    '        </div>';

                ht += '<div class="row goodsrow">\n' +
                    '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 goodsdiv" id="goodsname">'+ data1.goodsname +'</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 goodsdiv"><img src="/FVOnline/goods/getImg?goodsId='+ goodsid +'"></div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 goodsdiv"><span>￥'+ data1.price +'</span></div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2" id="goodsnum"><button id="jian">-</button><input type="text" id="buynum" value="'+ buynum +'"><button id="add">+</button></div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 goodsdiv" id="sumprice"><span class="sum">￥'+ parseFloat(data1.price) * parseFloat(buynum) +'</span></div>\n' +
                    '  </div>';

                ht += '<div class="row addressrow">\n' +
                    '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>\n' +
                    '                    <div class="col-xs-10 col-sm-10 col-md-10 col-lg-10">\n' +
                    '                        <div>请选择您的收货地址：</div>\n' + address +
                    '                        <div id="bei">备注：<input type="text" id="beizhu"></div>\n' +
                    '                        <div id="sumdiv">商品总价：<span class="sum">￥'+ parseFloat(data1.price) * parseFloat(buynum) +'</span></div>\n' +
                    '                        <div id="submitorder"><button>提交订单</button></div>\n' +
                    '                    </div>\n' +
                    '                </div>';

                $("#orderten").html(ht);

                $("#jian").click(function () {
                    if ($("#buynum").val() > 1){
                        $("#buynum").val(parseInt($("#buynum").val()) - 1);
                        $(".sum").html("￥"+ parseFloat(data1.price) * parseFloat($("#buynum").val()));
                    }
                });

                $("#add").click(function () {
                    if ($("#buynum").val() < data1.number){
                        $("#buynum").val(parseInt($("#buynum").val()) + 1);
                        $(".sum").html("￥"+ parseFloat(data1.price) * parseFloat($("#buynum").val()));
                    }
                });

                $("#buynum").change(function () {
                    $(".sum").html("￥"+ parseFloat(data1.price) * parseFloat($("#buynum").val()));
                });
                
                $("#submitorder").click(function () {

                    if ($('input[name="address"]:checked').val() == null){
                        alert("请选择收货地址！");
                    }else {

                        var shopid = data1.shopid;
                        var addressid = $('input[name="address"]:checked').val();
                        var buynum = $("#buynum").val();
                        var pricesum = parseFloat(data1.price) * parseFloat(buynum);
                        var remarks = $("#beizhu").val();

                        $.post("order/createOrder",{userId:userId,goodsid:goodsid,shopid:shopid,addressid:addressid,buynum:buynum,pricesum:pricesum,remarks:remarks},function (data3) {
                            data3 = JSON.parse(data3);

                            if (data3.code.toLowerCase() == "success"){
                                alert("提交订单成功，请在15分钟内完成支付");
                                location.href = "pay_success.html?order=" + data3.order + "&price=" + data3.price;
                            }else {
                                alert("提交订单失败，请稍后再试");
                            }

                        });

                    }
                });

            });
        });


    </script>

</body>
</html>